<?php
/*
 * Copyright (c) 2022.  Liuhongbin All rights reserved.
 * @domain http://dkyx.cc
 * @author liuhongbin <514660094@qq.com>
 */


namespace app\common\model\user;

use app\common\model\BaseModel;
use app\common\model\platform\Platform;

class UserPermission extends BaseModel
{
    protected $append = array(
        'user'
    );

    protected $type = array(
        'addons'   => 'json',
        'platform' => 'json'
    );

    public $platformPermission = array(
        1 => array(
            'user'     => '会员管理',
            'pay'      => '支付管理',
            'addon' => [
                'title' => '应用列表',
                'value' => [
                    'addon' => '应用列表',
                    'allAddon' => '全部应用',
                ]
            ],
            'page'     => '页面设置',
            'setting'  => '系统设置',
        ),
        2 => array(
            'addon' => [
                'title' => '应用列表',
                'value' => [
                    'addon' => '应用列表',
                    'allAddon' => '全部应用',
                ]
            ],
            'setting'       => '系统设置',
            'server'        => '服务端设置',
            'uploadSetting' => '上传设置',
            'upload'        => '小程序上传'
        ),
        3 => array(
            'addon' => [
                'title' => '应用列表',
                'value' => [
                    'addon' => '应用列表',
                    'allAddon' => '全部应用',
                ]
            ],
            'kf'      => [
                'title' => '微信客服',
                'value' => [
                    'kf-service' => '客服列表',
                    'kf-user'    => '用户列表',
                    'kf-msg'     => '消息列表',
                ]
            ],
            'setting' => '系统设置',
        ),
        4 => array(
            'user'     => '会员管理',
            'addon' => [
                'title' => '应用列表',
                'value' => [
                    'addon' => '应用列表',
                    'allAddon' => '全部应用',
                ]
            ],
            'setting'  => '系统设置',
        ),
        5 => array(
            'addon' => [
                'title' => '应用列表',
                'value' => [
                    'addon' => '应用列表',
                    'allAddon' => '全部应用',
                ]
            ],
            'setting'  => '系统设置',
        )
    );

    public function getPlatformData($plaid)
    {
        $type = (new Platform())->where(['plaid' => $plaid])->value('type');
        return getAddonAuthTree($this->platformPermission[$type]) ?? [];
    }

    public function user()
    {
        return $this->hasOne(User::class, 'uid', 'uid')->field(['username', 'avatar'])->bind(['username', 'avatar']);
    }

    /**
     *   根据权限获取平台ID数组
     * @param int $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPlaids($uid = 0)
    {
        if (!$uid) {
            return [];
        }
        $permission = $this->where(['uid' => $uid])->select();
        $plaids = [];
        foreach ($permission as $key => $value) {
            $plaids[] = $value['plaid'];
        }
        return $plaids;
    }

    /**
     * 根据权限获取应用列表名称对应数组
     * @param int $uid
     * @param int $plaid
     * @return array ['addon_name'=>['all']]
     */
    public function getAddonNames($uid = 0, $plaid = 0)
    {
        $uid = $uid ?: get_user()['uid'];
        $plaid = $plaid ?: get_plaid();
        $addons = $this->where(['uid' => $uid, 'plaid' => $plaid])->field('addons')->findOrEmpty()['addons'] ?? [];
        $res = [];
        foreach ($addons as $key => $value) {
            $res[] = $key;
        }
        return $res;
    }

    /**
     * 获取当前用户在当前平台ID下的所有应用权限
     * @param $uid
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAddonAllAuth($uid)
    {
        if (!$uid) {
            $uid = @get_user()['uid'];
        }
        $res = $this->where(['uid' => $uid, 'plaid' => get_plaid()])->find();
        if (!$res) {
            return [];
        } else {
            return $res->addons;
        }
    }
}
